package edu.se.jai.websocketgateway.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * WebSocket配置类
 * 
 * 配置STOMP协议和消息代理，支持：
 * - 点对点消息：/user/queue/response
 * - 房间广播消息：/room/{roomId}
 * - 全局广播消息：/topic/roomUpdate
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册STOMP端点，允许跨域访问
        registry.addEndpoint("/ws")
                .setAllowedOriginPatterns("*");
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 设置应用程序目标前缀，客户端发送消息时需要添加此前缀
        config.setApplicationDestinationPrefixes("/app");
        // 设置用户目标前缀，用于点对点消息
        config.setUserDestinationPrefix("/user");
        // 启用简单消息代理，支持 topic、queue、room 前缀
        config.enableSimpleBroker("/topic", "/queue", "/room");
    }
}

